<?php

/**
 * 会员注册登录
 */
class IndexAction extends CommonAction {
    
    public function __construct(){

        parent::__construct();

    }

    /**
     * 会员首页
     * @return [type] [description]
     */
    public function index() {

        $Member = D('Member');
        $Member->info(uid());

        $this->display(':index');
    }
    

    /**
     * 登录首页
     * @return [type] [description]
     */
    public function login(){
        $this->display(':login');
    }

    /**
     * 处理登录
     * @return [type] [description]
     */
    public function handleLogin(){

        $Member = D('Member');
        $res = $Member->login($_POST, $type = 1);
        // var_dump($res);exit;
        if($res > 0){
            redirect('/member.html');
        }else{
            $error = array(
                -1 => '用户不存在',
                -2 => '密码错误',
                -3 => '验证码错误',
                -4 => '账号被禁用',
            );
            $this->error($error[$res],'/member/login.html?');
        }
    }

    /**
     * 注册
     * @return [type] [description]
     */
    public function register() {
        
        $this->display(':register');
    }
    
    /**
     * 处理注册
     * @return [type] [description]
     */
    public function handleRegister(){

        $Member = D('Member');
        $res = $Member->register($_POST);
        // print_r($res);
        if(is_int($res) && $res > 0){   // 注册成功

            $user_info = $Member->info($res);

            $_SESSION['user'] = $user_info;

            $this->success('注册成功');
        }else{
            if(IS_AJAX){

            }else{
                $this->error($res);
            }
            
        }
    }
    
    /**
     * 邮箱验证
     * @return [type] [description]
     */
    public function checkEmail(){
        $Member = D('Member');
        $res = $Member->checkEmail($_GET['key']);
        if($res === 1){
            $this->success('验证成功','/member.html');
        }else{
            $error = array(
                -1 => '不存在该验证',

                -2 => '您已通过邮箱验证',
    
                -9 => '未知错误',
            );
            $this->error($error[$res],'/member.html');
        }
    }


    /**
     * 注销
     * @return [type] [description]
     */
    public function logout(){
        session('user',null);
        redirect('/member/login.html');
    }
    

    public function test(){
        // $mail_tpl_config = M('options')->where("option_name = 'member_email_active'")->getField('option_value');
        // $value = json_decode($mail_tpl_config,1);
        // // p($value);
        // $name = 'cyf';
        // $link = SITE_DOMAIN.'/member/index/checkEmail/key/'.'adafqwtadg';
        // $address = 'chenyifan@qiaogen.com';
        // $title = str_replace('#name#', $name, $value['title']);
        // $message = str_replace('#link#', $link, $value['template']);
        // $message = str_replace('#name#', $name, $message);
        // $res = SendMail($address,$title,$message);
        // var_dump($res);
    }

}

